clear all
global main "/Users/ingahei/UiO Dropbox/IngaB Heiland/Ais project/Repository"
global data "$main/data_intermediate"
global input "$main/data_input"
global figures "$main/figures"
cd "$data"

* convert monthly gravity data to quarterly
u "$input/gravdataFTA1319monthly.dta", clear
g lnd = ln(distw)
g q=ceil(month/3)
gcollapse (firstnm) lnd contig comlang_o (max) fta, by(year q iso_o iso_d) fast
save "$input/gravdataFTA1319quarterly.dta", replace


u "all_q", clear
merge m:1 iso_o iso_d q year using "$input/gravdataFTA1319quarterly.dta" //1:m because Serbia and MNE are one country in the gravity data
keep if _m==3
drop _m 

sort year q
egen perid=group(year q)
egen cid_i=group(iso_i)
egen cid_j=group(iso_j)

merge m:1 iso_i iso_j using "PESPbyHr_bil514" // PanExp1
keep if _merge==3
drop _merge 
rename XP PanExp1


g post=0
replace post=1 if (q>2 & year==2016) | year>2016

g postPanExp1=post*PanExp1
g postxlnd=post*lnd
g postxcontig=post*contig
g postxcomlang=post*comlang_o
g postNEOP_pre=post*steuNEOP_pre

foreach var of varlist value* {
	g ln`var'=ln(`var')
}

eststo main: reghdfe lnvalue_co postPanExp1 postNEOP_p  postxlnd  postxcont postxcoml  fta, absorb(cid_i#cid_j cid_i#perid cid_j#perid) cluster(cid_i cid_j) noconst
eststo no_control: reghdfe lnvalue_co postPanExp1  if _est_main==1, absorb(cid_i#cid_j cid_i#perid cid_j#perid) cluster(cid_i cid_j) noconst

esttab no_control main, b(3) ar2 se star( * .1 ** .05 *** .01)

sum lnvalue_co fta lnd contig comlang_o PanExp1 if _est_main==1
savesome iso_i iso_j year q _est_main value using "$data/estsample" if _est_main==1, replace 


forvalues j=1(1)28 {
g pXP`j'=0
replace pXP`j'=PanExp1 if perid==`j'
}


global coeflab "1 "2013_q1" 2 "2013_q2" 3 "2013_q3" 4 "2013_q4" 5 "2014_q1" 6 "2014_q2" 7 "2014_q3" 8 "2014_q4" 9 "2015_q1" 10 "2015_q2" 11 "2015_q3" 12 "2015_q4" 13 "2016_q1" 14 "2016_q2" 15 "2016_q3" 16 "2016_q4" 17 "2017_q1" 18 "2017_q2" 19 "2017_q3" 20 "2017_q4" 21 "2018_q1" 22 "2018_q2"  23 "2018_q3"  24 "2018_q4"  25 "2019_q1" 26 "2019_q2" 27 "2019_q3" 28 "2019_q4" "

replace pXP14=0


eststo: reghdfe lnvalue_co pXP1-pXP14 pXP15-pXP28 postxlnd fta_wto  postxcont postxcoml  postNEOP_pre, absorb(cid_i#cid_j cid_i#perid cid_j#perid) cluster(cid_i cid_j) noconst
coefplot, drop(pD* fta_wto  postxcontig postxcomlang postNEOP_pre postxlnd) scale(.5) vert xline(14) xlabel($coeflab, angle(90)) graphregion(color(white)) ylabel(-.3(.1).3) ci(90) omitted
graph export "$figures/coefplot_qrtly_v2.png", replace

forvalues j=1(1)28 {
g pD`j'=0
replace pD`j'=lnd if perid==`j'
}
eststo: reghdfe lnvalue_co pXP1-pXP14 pXP15-pXP28 pD* fta_wto  postxcont postxcoml  postNEOP_pre, absorb(cid_i#cid_j cid_i#perid cid_j#perid) cluster(cid_i cid_j) noconst
coefplot, drop(pD* fta_wto  postxcontig postxcomlang postNEOP_pre) scale(.5) vert xline(14) xlabel($coeflab, angle(90)) graphregion(color(white)) ylabel(-.3(.1).3) ci(90) omitted
graph export "$figures/coefplot_qrtly.png", replace


** robustness checks

merge m:1 iso_i iso_j using "$data/exposure_rob"
keep if _merge==3
drop _merge

g postPanExp1_av = post*mxXP
g postPanExp1_wav = post*wavXP
g postPanExp1_wgt = post*wgt_XPanama

merge m:1 iso_i iso_j using "$data/direct_links"
drop if _m==2
g direct=1 if _merge==3
replace direct=0 if direct==.
drop _merge

global bicoastal "USA MEX GTM HND NIC CRI PAN COL CAN"
g bicoastal =0
foreach x in $bicoastal {
replace bicoastal=1 if iso_i== "`x'"
replace bicoastal=1 if iso_j== "`x'"
}

g postPanExpAlt=post * ln(mDtt_XP)
replace postPanExpAlt=0 if postPanExpAlt==.





merge m:1 iso_d using "$data/distPA", keepusing(lndPA)
drop if _m==2
drop _m
rename lndPA lndPA_i
rename (iso_o iso_d) (iso_d iso_o)
merge m:1 iso_d using "$data/distPA", keepusing(lndPA)
drop if _m==2
rename lndPA lndPA_j
rename (iso_o iso_d) (iso_d iso_o)

gen ja = postPanExp1*lndPA_j
gen ia = postPanExp1*lndPA_i

gen ija=postPanExp1*ln(exp(lndPA_j)+exp(lndPA_i)) // sum of exporter's and importer's  distance from  Panama
egen mina=rowmin(lndPA_i lndPA_j)
replace mina = postPanExp1*mina // minimum sum of exporter's and importer's  distance from  Panama


** robustness table paper -- monthly below


eststo no_raw: reghdfe lnvalue_co_nr postPanExp1 postNEOP_p  postxlnd postxcont postxcoml  fta if _est_main==1, absorb(cid_i#cid_j cid_i#perid cid_j#perid) cluster(cid_i cid_j) noconst
eststo noq28: reghdfe lnvalue_co postPanExp1 postNEOP_p  postxlnd  postxcont postxcoml  fta if perid!=28 & _est_main==1, absorb(cid_i#cid_j cid_i#perid cid_j#perid) cluster(cid_i cid_j) noconst
eststo av: reghdfe lnvalue_co postPanExp1_av postNEOP_p  postxlnd postxcont postxcoml  fta if _est_main==1, absorb(cid_i#cid_j cid_i#perid cid_j#perid) cluster(cid_i cid_j) noconst
eststo wav: reghdfe lnvalue_co postPanExp1_wav postNEOP_p  postxlnd postxcont postxcoml  fta if _est_main==1, absorb(cid_i#cid_j cid_i#perid cid_j#perid) cluster(cid_i cid_j) noconst
eststo dtt: reghdfe lnvalue_co postPanExpAlt postNEOP_p  postxlnd postxcont postxcoml  fta if _est_main==1, absorb(cid_i#cid_j cid_i#perid cid_j#perid) cluster(cid_i cid_j) noconst



** additional robustness checks

eststo wgt: reghdfe lnvalue_co_nr postPanExp1_wgt postNEOP_p  postxlnd postxcont postxcoml  fta if _est_main==1, absorb(cid_i#cid_j cid_i#perid cid_j#perid) cluster(cid_i cid_j) noconst
eststo no_consign: reghdfe lnvalue postPanExp1 postNEOP_p  postxlnd postxcont postxcoml  fta if _est_main==1, absorb(cid_i#cid_j cid_i#perid cid_j#perid) cluster(cid_i cid_j) noconst
eststo no_direct: reghdfe lnvalue_co postPanExp1 postNEOP_p  postxlnd postxcont postxcoml  fta if direct!=1 & _est_main==1, absorb(cid_i#cid_j cid_i#perid cid_j#perid) cluster(cid_i cid_j) noconst
eststo no_bicoastal: reghdfe lnvalue_co postPanExp1 postNEOP_p  postxlnd postxcont postxcoml  fta if bicoastal!=1 & _est_main==1, absorb(cid_i#cid_j cid_i#perid cid_j#perid) cluster(cid_i cid_j) noconst

eststo distPA1: reghdfe lnvalue_co postPanExp1 ija  postNEOP_p  postxlnd postxcont postxcoml  fta  if  _est_main==1, absorb(cid_i#cid_j cid_i#perid cid_j#perid) cluster(cid_i cid_j) noconst

eststo distPA2: reghdfe lnvalue_co postPanExp1 mina  postNEOP_p  postxlnd postxcont postxcoml  fta  if  _est_main==1, absorb(cid_i#cid_j cid_i#perid cid_j#perid) cluster(cid_i cid_j) noconst







** monthly data

u "all_m", clear
merge m:1 iso_o iso_d period year using "$input/gravdataFTA1319monthly.dta" //1:m because Serbia and MNE are one country in the gravity data
keep if _m==3
drop _m 

egen cid_i=group(iso_i)
egen cid_j=group(iso_j)

merge m:1 iso_i iso_j using "PESPbyHr_bil514" // PanExp1
keep if _merge==3
drop _merge 
rename XP PanExp1

g post=0
replace post=1 if (month>6 & year==2016) | year>2016

g postPanExp1=post*PanExp1
g lnd=ln(distw)
g postxlnd=post*lnd
g postxcontig=post*contig
g postxcomlang=post*comlang_o
g postNEOP_pre=post*steuNEOP_pre

g lnvalue_co = ln(value_co)

merge m:1 iso_i iso_j year q using "$data/estsample"
drop _m

eststo monthly: reghdfe lnvalue_co postPanExp1 postNEOP_p  postxlnd  postxcont postxcoml fta if _est_main==1, absorb(cid_i#cid_j cid_i#period cid_j#period) cluster(cid_i cid_j) noconst


** Robustness table 1
esttab noq28 monthly no_raw no_direct no_bicoastal no_consign, b(3) ar2 se star( * .1 ** .05 *** .01)


** Robustness table 2
esttab av wav dtt wgt, b(3) ar2 se star( * .1 ** .05 *** .01)






** how much of global trade is covered by our estimation sample ?


import delim using "/Users/ingahei/UiO Dropbox/IngaB Heiland/Ais project/Repository/data_input/Comtrade_agg/TradeData_4_8_2024_14_36_53.csv", clear
keep *iso refyear cifvalue
rename refyear year
collapse (sum) cifvalue, by(year)
save "$data/temp", replace
  
u "$data/estsample", clear
collapse (sum) value, by(year)
merge 1:1 year using "$data/temp" 
collapse (sum) value cifvalue
g s=value/cifvalue

*share of global trade covered by our estimation sample 
sum s

 





